{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "12b20b08",
   "metadata": {},
   "source": [
    "# 单因素多因素回归\n",
    "\n",
    "指定数据，进行单因素、多因素回归算法，进行特征筛选。\n",
    "\n",
    "```python\n",
    "def variable_analysis(data: pd.DataFrame, features: Union[str, List[str]] = None, label_column: str = 'label',\n",
    "                      need_norm: Union[bool, List[bool]] = False, alpha=0.1, method='uni'):\n",
    "    \"\"\"\n",
    "    进行单因素或者多因素分析，请确保所有的分析特征都是数值类型的。\n",
    "    Args:\n",
    "        data: 数据\n",
    "        features: 需要分析的特征，默认除了ID和label_column列，其他的特征都进行分析。\n",
    "        label_column: 目标列\n",
    "        need_norm: 是否标准化所有分析的数据, 默认为False\n",
    "        alpha: CI alpha, alpha/2 %\n",
    "        method: 单因素回归分析还是多因素回归分析，uni 单因素回归，multi 多因素回归，默认单因素\n",
    "\n",
    "    Returns:\n",
    "\n",
    "    \"\"\"\n",
    " ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b6af319",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.api as sm\n",
    "from onekey_algo import OnekeyDS as okds\n",
    "\n",
    "# 替换你自己的数据目录\n",
    "my_dir = \"\" or okds.survival\n",
    "label_column = '' or 'result'\n",
    "data = pd.read_csv(my_dir)\n",
    "feature_column = ['chemotherapy', 'gender', 'duration', 'age', 'BMI', 'degree', 'Tstage', 'smoke']\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e65f76cb",
   "metadata": {},
   "source": [
    "# 单因素回归\n",
    "\n",
    "所有的特征，不进行任何交叉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e34bb0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from onekey_algo.custom.components.comp1 import variable_analysis, plot_HR\n",
    "uni = variable_analysis(data, features=feature_column, label_column=label_column, method='uni', need_norm=True, algo='logit')\n",
    "display(uni)\n",
    "plot_HR(uni)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "434ee686",
   "metadata": {},
   "source": [
    "# 多因素回归\n",
    "\n",
    "使用所有的数据进行交叉特征，计算得到。\n",
    "\n",
    "真实的场景中，先单因素，然后步进式多因素回归，进行筛选。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32c9e3a9",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from onekey_algo.custom.components.comp1 import variable_analysis, plot_HR\n",
    "uni = variable_analysis(data, features=['duration', 'age', 'Tstage'], label_column=label_column, method='uni', need_norm=True, algo='logit')\n",
    "display(uni)\n",
    "plot_HR(uni, figsize=(10, 3))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac30f71c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
